/**
 * Created by Administrator on 2016/9/12.
 */
$(function(){
    var win_w = $('.membercenter .balance .win').width();
    $('.membercenter .balance .win .animate').width(win_w*2);
    $('.membercenter .balance .win .animate p').width(win_w);
//    定时器切换
//    setInterval(function(){
//        $('.membercenter .balance .win .animate').animate({'left':-win_w},1000,function(){
//            $(this).children().first().prependTo($(this));
//            $(this).css('left',0)
//        });
//    },4000);

//    点击立即购买
    $('.gradecard .topmask').click(function(){
        var con_h = $('.gradecard .payment-layer .con').height();
        $('.gradecard .payment-layer .con').animate({'bottom':-con_h},300,function(){
            $('.payment-layer').fadeOut();
        });
    });
    $('#purchase').click(function(){
        $('.payment-layer').fadeIn();
        $('.gradecard .payment-layer .con').animate({'bottom':'0px'},300);
    });
//    微信支付宝选择
    $('.gradecard .payment-layer .con .pay-mode .public .radio').click(function(){
        $(this).attr('src','img/icon41.png').parent().siblings('.public').children('.radio').attr('src','img/icon40.png');
        $(this).addClass('hover').parent().siblings('.public').children('.radio').removeClass('hover');
    });
//    充值 点击推荐金额
    $('.membercenter .recommendnum p').click(function(){
        var text = $(this).data('num');
        $('.membercenter .personaldata .moneynum input').val(text);
    });
//    点击卡进入详情
    $(document).on('click','.membercenter .membercard .allcard .content .box',function(){
        var id = $(this).data('id');
        location.href = 'gradecard.html?id='+id;
    });
    var success_url = 'http://www.ekangjiuyi.net/EKang2.0/hospital_nurse/gradecard-detail.html';
//    点击抢购会员卡按钮
    var uid = $.cookie("local_userId");
    $(document).on('click','#cardlist .content .right .num button',function(){
        var id = $(this).data('id');
        membercenter.getcode('diagnosis/getcharge/',{order_id: 0,product_id:id,pay_method:"wx_pub",success_url:success_url,uid:uid});
        //location.href = 'gradecard-detail.html?id='+id;
    });
//    点击促销会员卡

    $(document).on('touchstart','.membercenter .couponcard .public',function(){
        var id = $(this).data('id');
        if($.cookie("is_wechat")==1){
            location.href = 'http://www.ekangjiuyi.net/EKang2.0/hospital_nurse/gradecard-detail.html?product_id='+id
        }else{
            membercenter.getcode('diagnosis/getcharge/',{order_id: 0,product_id:id,pay_method:"wx_pub",success_url:success_url,uid:uid});
        }

        //location.href = 'gradecard-detail.html?id='+id
    });
});
url = 'http://ekang.ren/'
membercenter = {
//    get
    get : function (texturl,data,success){
        $.ajax({
            type : 'get',
            url : url+texturl,
            dataType : 'jsonp',
            data : data,
            success : function(response) {
                success(response);
            }
        });
    },
//    获取连接参数
    getQueryString : function(name){
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(decodeURI(r[2])); return null;
    },
//    倒计时
    showtime : function(time){
        var www = document.getElementById('countdown');
        //var Newtime = new Date();
        //var endtime = new Date(time);
        //var lefttime=parseInt((endtime.getTime()-Newtime.getTime())/1000);
        var lefttime = time;
        if(lefttime>0){
            var date  =  parseInt(lefttime/(60*60*24));
            var hours   =  parseInt(lefttime/(60*60)%24);
            var minute= parseInt(lefttime/60%60);
            var second=  parseInt(lefttime%60);
            www.innerHTML = '<div class="date public"><span>0</span>'+date+'</div><div class="dd">天</div><div class="hour public"><span>0</span>'+hours+'</div><div class="dd">时</div><div class="minute public"><span>0</span>'+minute+'</div><div class="dd">分</div><div class="second public"><span>0</span>'+second+'</div><div class="dd">秒</div>'
            if(second>9){
                $('#countdown .second span').hide();
            }
            if(minute>9){
                $('#countdown .minute span').hide();
            }
            if(hours>9){
                $('#countdown .hour span').hide();
            }
            if(date>9){
                $('#countdown .date span').hide();
            }
        }else{
            www.innerHTML = '<div class="date public"><span>00</span></div><div class="dd">天</div><div class="hour public"><span>00</span></div><div class="dd">时</div><div class="minute public"><span>00</span></div><div class="dd">分</div><div class="second public"><span>00</span></div><div class="dd">秒</div>'

        }
    },
//    产品列表
    productlist : function(url,data){
        membercenter.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                var pro_list = data.pro_list;

                $.each(pro_list,function(i,t){
                    $('.gradecard .cardlist').append('<div class="con" data-id="'+ t.product_id+'"><div class="cardtype"><div class="left"><img src="'+ t.pic+'" alt=""><p class="name">'+ t.product_name+'</p></div><div class="right"><span class="span2">￥'+ t.total_amount+'</span></div></div><div class="servicecontent">'+ t.title_html_content+'</div><button class="purchase ui-btn ui-shadow ui-corner-all">立即抢购</button></div>');
                });
                $('.gradecard .welfaretext .text').html(data.service_content);
                $('.gradecard .remarks .text').html(data.remark);
            }
            else{
                alert(res.msg);
            }
        });
    },
//    产品详情
    produdatail : function(url,data){
        membercenter.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                $('.gradecard .countdown').attr('data-time',data.end_promotion);
                $('.gradecard .countdown').attr('daat-mark',data.product_category);
                if(data.product_category==0){
                    $('.gradecard .countdown').hide();
                }
                $('.gradecard .cardlist .con .cardtype .left img').attr('src',data.pic);
                $('.gradecard .cardlist .con .cardtype .left .name').html(data.product_name);
                $('.gradecard .cardlist .con .cardtype .right .span2').html('￥'+data.total_amount);
                $('.gradecard .cardlist .servicecontent').html(data.title_html_content);
                $('.gradecard .welfaretext .text').html(data.service_content);
                $('.gradecard .remarks .text').html(data.remark);
            }
            else{
                alert(res.msg);
            }
        })
    },
//    促销卡列表页
    promotion : function(url,data){
        membercenter.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                //console.log(page_size+'-'+data.promotion_card.length)
                if(data.promotion_card.length<page_size){
                  $('.infinite-scroll-preloader').html('暂无加载数据').css('color','#666');
                    $('.infinite-scroll-preloader').css('line-height','40px');
                }
                $('.panicbuying .countdown').attr('data-time',data.end_promotion);
                $.each(data.promotion_card,function(i,t){
                    $('#cardlist').append('<div data-id="'+ t.product_id+'" class="content"><img src="'+ t.thumb_img+'" class="card_pic"><div class="right"><div class="name"><img src="'+ t.pic+'"><p>'+ t.product_name+'</p></div><div class="price"><p class="new">￥'+ t.total_amount+'</p><del>'+ t.origin_total_amount+'</del></div><div class="num"><span>售出 '+ t.sold+'</span><button data-id="'+ t.product_id+'" class=" ui-btn ui-shadow ui-corner-all">马上抢</button></div></div></div>');
                });
            }
            else{
                alert(res.msg);
            }

        });

    },
//    webViewJavaScript初始化
    connectWebViewJavascriptBridge :  function(callback) {
        if (window.WebViewJavascriptBridge) {
            callback(WebViewJavascriptBridge);
        } else {
            document.addEventListener('WebViewJavascriptBridgeReady', function() {callback(WebViewJavascriptBridge);}, false);
        }
    },
//    用户信息
    UserMemberInfo : function(url,data){
        membercenter.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                $('.membercenter .personaldata .userinfor .left .head_pic,.gradecard .payment-layer .con .userinfor .left .head_pic').attr('style',' background: url('+ data.avatar_file+') center; background-size: cover;');
                $('.membercenter .personaldata .userinfor .left .name_time .name,.gradecard .payment-layer .con .userinfor .left .name_time .name').html(data.user_name);
                $('.membercenter .personaldata .userinfor .left .name_time .time,.gradecard .payment-layer .con .userinfor .left .name_time .time').html(data.service_start+'-'+data.service_end);
                $('.membercenter .personaldata .userinfor .right img,.gradecard .payment-layer .con .userinfor .right img').attr('src',data.max_product_img);
                $('.membercenter .personaldata .userinfor .right p,.gradecard .payment-layer .con .userinfor .right p').html(data.product_name);
                $('.membercenter .personaldata .remarks').html('注：'+data.promotion_text);
                $('.gradecard .payment-layer .con .time .p2').html(data.service_start_1+'-'+data.service_end_1)
            }
            else{
                alert(res.msg);
            }
        });
    },
    getcode : function(url,data){
        membercenter.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                if(res==0){
                    alert('ok')
                }else{
                    console.log(res.msg)
                }
                var url = res.data.location_url;
                location.href = url;
            }
            else{
                alert(res.msg);
            }
        });
    }
}